package com.stx.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Suntingxing
 * @date 2021/11/5 11:13
 */
public class Calculator {
    private static int result;

    public void add(int n){
        result = result +n;
    }

    public int add1(int n){
        result = result +n;
        return result;
    }

    public void substract(int n){
        result = result -1;
    }

    public void multiply(int n){
        result = result*n;
    }

    public void divide(int n){
        System.out.println("搞不了");
    }

    public void square(int n){
        result = n*n;
    }

    public void squareRoot(int n){
        for (;;); // bug构建
    }

    public  double MySqrt(int value, double t){
        if (value < 0 || t<0)
            return 0;
        double left = 0;
        double right = value;
        double mid = (right + left) / 2;
        double offset = 2*t ;
        while (offset>t){
            double temp = mid*mid;
            if (temp > value){
                right = (left + right) / 2;
                offset = temp - value;
            }
            if (temp <= value){
                left = (left + right) / 2;
                offset = value - temp;
            }
            mid = (left + right) / 2;
        }
        return mid;
    }

    public double sqrt(double number) {
        double t;
        if(number <=0){
            number = -number;
            result = (int) (number / 2);
            do {
                t = result;
                result = (int) ((t + (number / t)) / 2);
            } while ((t - result) != 0);
            return result;
        }else{
            result = (int) (number / 2);
            do {
                t = result;
                result = (int) ((t + (number / t)) / 2);
            } while ((t - result) != 0);
            return result;
        }
    }

    public void setResult(int n){
        result =n;
    }

    public int getResult(){
        return result;
    }
}
